//二、基于以下代码完成下面四个练习
const fp = require('loadsh/fp');
// 数据 horsepower 马力  dollar_value 价格  in_stock 库存

const cars = [
  { name: 'Zhang San', horsepower: 600, dollar_value: 60000, in_stock: true },
  { name: 'Li Si', horsepower: 800, dollar_value: 80000, in_stock: false }
];

// 问题1

//首先柯里化prop函数降元
let prop = fp.curry(fp.prop('in_stock'));
//函数组合
let compose = fp.flowRight(prop, fp.last);
compose(cars);

// 问题2

//首先柯里化prop函数降元
let prop2 = fp.curry(fp.prop('name'));
//函数组合
let compose2 = fp.flowRight(prop2, fp.first);
compose2(cars);

// 问题3

let _average = function (xs) {
  return fp.reduce(fp.add, 0, xs) / xs.length;
};
//log函数用于检测组合函数中的错误
function log(value) {
  console.log(value, 'log');
  return value;
}
//将map函数降元
let map = fp.curry(fp.map(car => car.dollar_value));
let compose3 = fp.flowRight(_average, log, map);
compose3(cars);


// 问题四

let _underscore=fp.replace(/\W+/g,'_')
let nameMap = fp.curry(fp.map(car => car.name));
let sanitizeNames=fp.flowRight(fp.map(fp.flowRight(fp.toLower,_underscore)),nameMap);
sanitizeNames(cars)

